#include "stm32f10x.h"                  // Device header
#include "Key.h"
#include "OLED.h"


uint8_t hour,min,sec;
void Show_StopClock_UI(void)
{
	OLED_ShowImage(0,0,16,16,Image_Return);
	OLED_Printf(32,20,OLED_8X16,"%02d:%02d:%02d",hour,min,sec);
	OLED_ShowString(0,48,"开始",OLED_8X16);
	OLED_ShowString(48,48,"停止",OLED_8X16);
	OLED_ShowString(96,48,"清除",OLED_8X16);
}


uint16_t StopClock_Count;
uint8_t start_timing_flag;	//1:开始,2:停止，3：清除
void StopClock_Tick(void)
{
	if(start_timing_flag==1)
	{	
		StopClock_Count++;
		if(StopClock_Count>=1000)
		{
			StopClock_Count=0;
			sec++;
			if(sec>=60)
			{
				sec=0;
				min++;
				if(min>=60)
				{
					min=0;
					hour++;
					if(hour>99)
					{
						hour=0;
					}
				}
			
			}
			
		}
	}
	else if(start_timing_flag==3)
	{
		StopClock_Count=0;
		hour=0;
		min=0;
		sec=0;
	}
	
}

uint8_t stopclock_flag=1;
extern uint8_t KeyNum;
uint8_t StopClock(void)
{
	while(1)
	{
		KeyNum=Key_GetNum();
		uint8_t stopclock_flag_temp=0;
		
		
		if(KeyNum==1)		//上移
		{	
			stopclock_flag--;
			if(stopclock_flag<=0)stopclock_flag=4;
		}else if(KeyNum==2)	//下移
		{	
			stopclock_flag++;
			if(stopclock_flag>=5)stopclock_flag=1;
			
		}else if(KeyNum==3)	//确定
		{	
			OLED_Clear();
			OLED_Update();
			stopclock_flag_temp=stopclock_flag;
		}
		
		switch(stopclock_flag)
		{
			case 1:
				Show_StopClock_UI();
				OLED_ReverseArea(0,0,16,16);
				OLED_Update();
				break;
			case 2:
				Show_StopClock_UI();
				OLED_ReverseArea(0,48,32,32);
				OLED_Update();
				break;
			case 3:
				Show_StopClock_UI();
				OLED_ReverseArea(48,48,32,32);
				OLED_Update();
				break;
			case 4:
				Show_StopClock_UI();
				OLED_ReverseArea(96,48,32,32);
				OLED_Update();
				break;
		}
	
		if(stopclock_flag_temp==1){return 0;}
		else if(stopclock_flag_temp==2){start_timing_flag=1;}
		else if(stopclock_flag_temp==3){start_timing_flag=2;}
		else if(stopclock_flag_temp==4){start_timing_flag=3;}
	}
}
